package com.huixuebao.exchange.service.Impl;

import cn.hutool.core.bean.BeanUtil;
import cn.hutool.core.util.StrUtil;
import com.baomidou.mybatisplus.core.conditions.query.LambdaQueryWrapper;
import com.huixuebao.common.bean.ApiResult;
import com.huixuebao.common.enumeration.ResultCodeEnum;
import com.huixuebao.exchange.dto.CategoryDTO;
import com.huixuebao.exchange.dto.CategoryResultDTO;
import com.huixuebao.exchange.entity.CategoryEntity;
import com.huixuebao.exchange.mapper.CategoryMapper;
import com.huixuebao.exchange.service.CategoryService;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.stereotype.Service;

import java.util.ArrayList;
import java.util.List;
@Service
public class CategoryServiceImpl implements CategoryService {

    @Autowired
    CategoryMapper categoryMapper;

    @Override
    public ApiResult<List<CategoryDTO>> categoryTree() {
        ApiResult apiResult = new ApiResult();
        try {
            List<CategoryDTO> categories = listDTO("0");
            for (CategoryDTO dto : categories) {
                getChilds(dto);
            }
            CategoryResultDTO categoryResultDTO = new CategoryResultDTO();
            categoryResultDTO.setData(categories);
            apiResult.setData(categoryResultDTO);
            apiResult.setSuccess(true);
            apiResult.setCode(ResultCodeEnum.SUCCESS.getCode());
            apiResult.setMessage("获取课程字典表成功！！");
        }catch (Exception e){
            apiResult.setSuccess(false);
            apiResult.setCode(ResultCodeEnum.FAILURE.getCode());
            apiResult.setMessage("获取课程字典表失败！！");
        }
        return apiResult;
    }

    private List<CategoryDTO> listDTO(String pid) {
        List<CategoryEntity> list = categoryMapper.selectList(new LambdaQueryWrapper<CategoryEntity>().eq(CategoryEntity::getPid, pid).orderByDesc(CategoryEntity::getId));
        List<CategoryDTO> result = new ArrayList<>();
        list.forEach(entity -> {
            CategoryDTO dto = new CategoryDTO();
            BeanUtil.copyProperties(entity, dto);
            result.add(dto);
        });
        return result;
    }

    private void getChilds(CategoryDTO dto) {
        List<CategoryDTO> childs = listDTO(dto.getId());
        dto.setChildren(childs);
        for (CategoryDTO childCategory: childs) {
            getChilds(childCategory);
        }
    }
    @Override
    public ApiResult<List<CategoryDTO>> listAllCategoryByPid(String pid) {
        if (StrUtil.isBlank(pid)) {
            pid = "0";
        }
        List<CategoryEntity> list = new ArrayList<>();
        List<CategoryDTO> result = new ArrayList<>();
        List<CategoryEntity> categories = categoryMapper.selectList(new LambdaQueryWrapper<CategoryEntity>().eq(CategoryEntity::getPid, pid));
        list.addAll(categories);
        categories.stream().forEach(child -> {
            List<CategoryEntity> childCategories = categoryMapper.selectList(new LambdaQueryWrapper<CategoryEntity>().eq(CategoryEntity::getPid, child.getId()));
            list.addAll(childCategories);
        });

        list.forEach(categoryEntity -> {
            result.add(new CategoryDTO().setId(categoryEntity.getId()).setName(categoryEntity.getName()));
        });
        CategoryEntity parentCategory = categoryMapper.selectById(pid);
        result.add(new CategoryDTO().setId(parentCategory.getId()).setName(parentCategory.getName()));
        ApiResult apiResult = new ApiResult();
        apiResult.setData(result);
        return apiResult;
    }
}
